package cn.usian.service.serviceimpl;

import cn.usian.mapper.UserMapper;
import cn.usian.pojo.User;
import cn.usian.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public Page<User> findPage(Integer pageIndex, String name) {
        Page<User> page = new Page<>(pageIndex,3);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (name !=null && !name.equals("")){
            queryWrapper.like(User::getUsername,name);
        }
        Page<User> page1 = userMapper.selectPage(page, queryWrapper);
        return page1;
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void delUser(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void editUser(User user) {
        userMapper.updateById(user);
    }
}
